var ProcessDeployManaged = function() {
	return {
		datatable : null,
		init : function() {
			this.initSearch();
			this.initListener();
		},
		initSearch : function() {
			if (!jQuery().dataTable) {
				return;
			}
			var pictureTable = $("#processDeployListTable").dataTable({
				"sAjaxSource" : ctx + "/process/deploy/search?now=" + new Date().getTime(),
				/* 使用post方式 */
				"fnServerData" : function(sSource, aoData, fnCallback) {
					var postData = {
						page : 1,
						start : 0,
						rows : 10,
						params : {}
					};
					// 参数
					for (var i = 0, size = aoData.length; i < size; i++) {
						if (aoData[i].name == 'iDisplayLength') {
							postData.rows = aoData[i].value;
						} else if (aoData[i].name == 'iDisplayStart') {
							postData.start = aoData[i].value;
						} else if (aoData[i].name == 'sEcho') {
							postData.page = aoData[i].value;
						}
					}

					if($('#name').val()){
						postData.params.processName = $('#name').val();
					}
					
					$.ajax({
						"dataType" : 'json',
						"type" : "POST",
						"cache": false,
						"url" : sSource,
						"data" : postData,
						"success" : function(data, textStatus, jqXHR){
							fnCallback(data.data, textStatus, jqXHR);
						}
					});
				},
				"bAutoWidth":false,
				"aoColumnDefs" : [
						
						{
							"data" : "index",
							"aTargets" : [ 0 ]
						},{
							"data" : "id",
							"aTargets" : [ 1 ]
						},
						{
							"data" : "name",
							"aTargets" : [ 2 ]
						},
						{
							"data" : "deploymentTime",
							"aTargets" : [ 3 ]
						},
						{
							"aTargets" : [ 4 ],
							"mRender" : function(data, type, full) {
								var btns =  '';
								btns +=  Permission.createPermElement('process-deploy:list:del',' <button title="'+messages.public_button_del+'" class="btn btn-sm-table  btn-danger" name="del" target_id="'+full.id+'">删除</button>');
								return btns;
							}
						} ]
			});
			this.datatable = pictureTable;
		},
		reSearch:function(){
        	this.datatable.fnDraw();
        },removeClass:function(){//去除Form表单的验证样式
        	$('.form-group','#actForm').removeClass('has-success')
            .removeClass('has-error');
    		 $('span').remove('.help-block');
        },initListener : function() {
			/*
			 * 查询
			 */
			$("#searchBtn").click(function() {
				ProcessDeployManaged.reSearch();
				return false;
			});

			/*
			 * 清除
			 */
			$("#clearBtn").click(function() {
				$("#searchForm")[0].reset();
				return false;
			});

			/*
			 * 添加
			 */
			$("#addBtn").click(function() {
				ProcessDeployManaged.removeClass();
				$("div[name=editDlg] h4.modal-title").html("新增部署文件");
				$("#actForm")[0].reset();
				//$("#actForm").attr('action', ctx + "/process/deploy/addDeploy?now="+new Date().getTime());
				$("#actForm").find("input[name='id']").val("");
				$("#editDlg").modal("show");
			});
			
			$("#actFormBtn").click(function(){
				$.ajaxFileUpload({
					fileElementId : 'deployFile',// 文件选择框的id属性
					dataType : 'json',// 服务器返回的格式，可以是json
					url : ctx + "/process/deploy/addDeploy?now="+new Date().getTime(),// 需要链接到服务器地址
					success : function(data, status) {
						BootstrapDialog.show({
							title : messages.public_cue_message,
							message : "部署成功",
							buttons : [ {
								label : messages.public_cue_sure,
								action : function(dialog) {
									ProcessDeployManaged.reSearch();
									dialog.close();
								}
							} ]
						});
						$("#editDlg").modal("hide");
					}
				});
			});
			$('#processDeployListTable tbody')
				.on('click', 'button[name="del"]',function() {
					var id = $(this).attr("target_id");
					ProcessDeployManaged.del(id);
				});
		},
		/**
		 * 删除
		 */
		del : function(id) {
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : messages.public_cue_warning,
				message : messages.public_cue_delSure,
				buttons : [ {
					label : messages.public_cue_close,
					cssClass: 'btn btn-sm btn-default',
					action : function(dialogItself) {
						dialogItself.close();
					}
				}, {
					label : messages.public_cue_sure,
					cssClass : 'btn btn-sm btn-primary',
					action : function(dialogItself) {
						ProcessDeployManaged.sureDel(id);
						dialogItself.close();
					}
				} ]
			});
		},
		sureDel : function(id){
			$.ajax({
				"dataType" : 'json',
				"cache": false,
				"type" : "POST",
				"data" : {},
				"url" : ctx + '/process/deploy/delete/'+id+"?now="+new Date().getTime(),
				"success" : function(data, textStatus, jqXHR){
					if (data.type == 'success') {
						BootstrapDialog.show({
							title : messages.public_cue_message,
							message : messages.public_cue_delSuccess,
							buttons : [ {
								label : messages.public_cue_sure,
								action : function(dialog) {
									ProcessDeployManaged.reSearch();
									dialog.close();
								}
							} ]
						});
					} else {
						BootstrapDialog.show({
							title : messages.public_cue_message,
							message : data.message,
							buttons : [ {
								label : messages.public_cue_sure,
								action : function(dialog) {
									dialog.close();
								}
							} ]
						});
					}
				}
			});
		}
	};

}();